package com.example.dyy.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.dyy.entity.Movie;
import com.example.dyy.util.DBUtil;

@WebServlet("/movie/list")
public class MovieListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        List<Movie> movies = new ArrayList<>();
        
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery("SELECT * FROM movie")) {
            
            while (rs.next()) {
                Movie movie = new Movie();
                movie.setId(rs.getInt("id"));
                movie.setName(rs.getString("name"));
                movie.setDuration(rs.getInt("duration"));
                movie.setType(rs.getString("type"));
                movie.setDescription(rs.getString("description"));
                movie.setPrice(rs.getDouble("price"));
                movies.add(movie);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        request.setAttribute("movies", movies);
        request.getRequestDispatcher("/movie/list.jsp").forward(request, response);
    }
} 